package com.keshihao.eduservice.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.keshihao.commonutils.R;
import com.keshihao.eduservice.constant.Course;
import com.keshihao.eduservice.entity.EduCourse;
import com.keshihao.eduservice.entity.chapter.ChapterVo;
import com.keshihao.eduservice.entity.vo.CourseInfoVo;
import com.keshihao.eduservice.entity.vo.CoursePublishVo;
import com.keshihao.eduservice.entity.vo.CourseQuery;
import com.keshihao.eduservice.service.EduCourseService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author keshihao
 * @since 2020-05-25
 */
@CrossOrigin
@RestController
@RequestMapping("/eduservice/course")
public class EduCourseController {

    @Autowired
    private EduCourseService courseService;

    // 添加课程基本信息的方法
    @PostMapping("/addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        String id = courseService.saveCourseInfo(courseInfoVo);
        return R.ok().data("courseId",id);
    }

    @GetMapping("/getCourseById/{courseId}")
    public R getCourseById(@PathVariable String courseId) {

        // 根据课程id查询课程信息
        CourseInfoVo courseInfoVo = courseService.getCourseById(courseId);

        return R.ok().data("courseInfo",courseInfoVo);

    }

    // 修改课程信息
    @PostMapping("/updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {

        courseService.updateCourseInfo(courseInfoVo);

        return R.ok();

    }

    // 根据课程id查询课程确认信息
    @GetMapping("getCoursePublishInfo/{id}")
    public R getCoursePublishInfo(@PathVariable String id) {

      CoursePublishVo coursePublishInfo = courseService.getCoursePublishInfo(id);

      return R.ok().data("coursePublishInfo",coursePublishInfo);

    }

    // 课程最终发布
    // 修改课程状态
    @PostMapping("/publishCourse/{id}")
    public R publishCourse(@PathVariable String id) {

      EduCourse eduCourse = new EduCourse();

      eduCourse.setId(id);

      eduCourse.setStatus(Course.COURSE_NORMAL);

      courseService.updateById(eduCourse);

      return R.ok();

    }

  @ApiOperation(value = "分页课程列表")
  @GetMapping("{page}/{limit}")
  public R pageQuery(
    @ApiParam(name = "page", value = "当前页码", required = true)
    @PathVariable Long page,

    @ApiParam(name = "limit", value = "每页记录数", required = true)
    @PathVariable Long limit,

    @ApiParam(name = "courseQuery", value = "查询对象", required = false)
      CourseQuery courseQuery){

    Page<EduCourse> pageParam = new Page<>(page, limit);

    courseService.pageQuery(pageParam, courseQuery);

    List<EduCourse> records = pageParam.getRecords();

    long total = pageParam.getTotal();

    return  R.ok().data("total", total).data("rows", records);

  }

  // 删除课程
    @DeleteMapping("/{courseId}")
    public R deleteCourse(@PathVariable String courseId) {
        courseService.removeCourse(courseId);
        return R.ok();
    }

}

